package com.common.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @classDescription:
 * @author:xiayingjie
 * @createTime:16/7/15
 */
//实现ServeletContainer 默认tomcat启动会调用其实现类 替换web.xml(只支持servlet3.0容器)
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    //---contextLoaderListener
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{AppConfig.class,DataSourceConfig.class,PropertiesConfig.class};
    }

    //dispatchServelt
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //设置utf-8编码过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return new Filter[]{characterEncodingFilter};
    }
}
